import moment from "moment";

export default class TimeUtil{

    static HOURS = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]

    static timestamp2YMDHMS(timestamp){
        if(!timestamp) return "";
        let temp = Number(timestamp)
        const date = new Date(temp);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

    /**
     * 判断时间1是否在时间2之前（包括等于）
     * @param time1 时间1
     * @param time2 时间2
     */
    static isTime1BeforeEqTime2(time1,time2){
        let moment1 = moment(time1);
        let moment2 = moment(time2);
        return moment1.isBefore(moment2) || moment1.isSame(moment2);
    }

    /**
     * 判断时间1是否在时间2之前
     * @param time1 时间1
     * @param time2 时间2
     */
    static isTime1BeforeTime2(time1,time2){
        let moment1 = moment(time1);
        let moment2 = moment(time2);
        return moment1.isBefore(moment2);
    }

    /**
     * 计算当前小时之前（包含自己）的小时数组
     * @param time 当前时间
     * @return {*[]} 小时数组
     */
    static getBeforeHours(time){
        let hour = moment(time).hour();
        let arr = []
        for (let h of this.HOURS) {
            if (h <= hour){
               arr.push(h);
            }
        }
        return arr;
    }
}
